<?php
session_start();

if (!isset($_SESSION['AdminId']) || $_SESSION['AdminId'] == "") {
    echo "<script type='text/javascript'>location.href='admin.php'</script>";
}

require_once 'common/config.php';
require_once 'common/conn.php';
require_once 'common/function.php';

require_once 'db/account.db.php';

$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$adjacents = isset($_GET['adjacents']) ? intval($_GET['adjacents']) : 0;

$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : "";

if ($page <= 0) {
    $page = 1;
}
if ($adjacents <= 0) {
    $adjacents = 4;
}

$startPage = $page - 1;
$limit = 50;

$dbc = new DBC();
$accountDb = new AccountDb($dbc);

$start = $startPage * $limit;
$array = $accountDb->query($keyword, $start, $limit);
$numberOfObject = $accountDb->getItemNumber($keyword);

$dbc->close();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>已审核老师列表</title>
        <meta charset="UTF-8">
        <link href="<?php echo $flatUi; ?>dist/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <script src="<?php echo $flatUi; ?>js/jquery-1.11.3.js"></script>
        <script type="text/javascript">
            $().ready(function() {
                btnClickHandler();
            });

            function btnClickHandler() {
                $(".btn").click(function() {
                    var idStr = $(this).attr("id");
                    if (idStr.indexOf("gold_") != -1) {
                        var id = idStr.replace("gold_", "");
                        location.href = "gold_edit.php?id=" + id;
                    } else if (idStr.indexOf("disable_") != -1) {
                        var id = idStr.replace("disable_", "");
                        location.href = "../handle/teacher.handle.php?userId=" + id + "&action=disable&status=2";
                    } else if (idStr.indexOf("create") != -1) {
                        location.href = "item_edit.php";
                    }
                });
            }

        </script>
    </head>
    <body>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>账号</th>
                    <th>名称</th>
                    <th>类型</th>
                    <th>最近登陆时间</th>
                    <th>退出时间</th>
                    <th>上线次数</th>
                    <th>余额</th>
                    <th>赠送</th>
                </tr>
            </thead>
            <tbody id="tbody">
                <?php
                for ($i = 0; $i < count($array); $i++) {
                    $j = $startPage * $limit + $i + 1;
                    $account = $array[$i];
                    $accUnique = $account['AccUnique'];
                    $AccountID = $account['AccountID'];
                    $UserType = $account['UserType'];
                    $Name = $account['Name'];
                    $recenttime = $account['recent'];
                    $outtime = $account['outTime2'];
                    $userip = $account['userip'];
                    $LoginTimeSum = $account['LoginTimeSum'];
                    $balance = $account['balance'];
                    ?>
                    <tr>
                        <td><?php echo $j; ?></td>
                        <td><?php echo $AccountID; ?></td>
                        <td><?php echo $Name; ?></td>
                        <td><?php echo $UserType; ?></td>
                        <td><?php echo $recenttime; ?></td>
                        <td><?php echo $outtime; ?></td>
                        <td><?php echo $LoginTimeSum; ?></td>
                        <td><?php echo $balance; ?></td>
                        <td><input id="gold_<?php echo $accUnique; ?>" type="button" class="btn btn-primary" value="赠送"/></td>
                    </tr>
                <?php } ?>
                <tr id="bottom_tr"></tr>
            </tbody>
        </table>
        <div style="width: 100%; text-align: center; padding-bottom: 20px;">
            <?php
            $tpages = intval($numberOfObject / $limit) + (intval($numberOfObject % $limit) == 0 ? 0 : 1);
            $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
            echo paginate_three($reload, $page, $tpages, $adjacents);
            ?></div>
    </body>
</html>